local fangxu=fk.CreateSkill{
    name="history__fangxu",
}
Fk:loadTranslationTable{
    ["history__fangxu"] = "芳许",
    [":history__fangxu"] = "每回合限两次，你可以将一张本回合获得的手牌当普通【杀】或【闪】使用。你以此法使用牌时秘密选择一项：1.若此牌造成伤害，你弃置受伤角色的至多两张牌；2.若此牌未造成伤害，当前回合角色摸两张牌。",
    
    ["fangxu_damage"] = "若此牌造成伤害，你弃置受伤角色的至多两张牌",
    ["fangxu_draw"] = "若此牌未造成伤害，当前回合角色摸两张牌",

    ["#history__fangxu-choose"] = "芳许：你可以将一张本回合获得的手牌当普通【杀】或【闪】使用。你以此法使用牌时秘密选择一项：1.若此牌造成伤害，你弃置受伤角色的至多两张牌；2.若此牌未造成伤害，当前回合角色摸两张牌。",
    ["@@history__fangxu_mark-turn"] = "芳许",
    ["#history__fangxu-choice"] = "芳许：你秘密选择一项：1.若此牌造成伤害，你弃置受伤角色的至多两张牌；2.若此牌未造成伤害，当前回合角色摸两张牌。",
}

fangxu:addEffect("viewas", {
    anim_type = "control",
    prompt = "芳许：你可以转化一张本回合获得的牌",
    pattern = "jink,slash",
    interaction = function(self, player)
        local all_names = {"slash","jink"}
        local names = player:getViewAsCardNames(fangxu.name, all_names)
        if #names == 0 then return end
        return UI.CardNameBox { choices = names, all_choices = all_names }
      end,
    card_filter = function(self, player, to_select, selected)
      if #selected > 0 or not self.interaction.data then return false end
        local card = Fk:getCardById(to_select)
        if  card:getMark("@@history__fangxu_mark-turn")>0 then
          return true
        end
      return false
    end,
    view_as = function(self, player,cards)
      if #cards == 0 or not self.interaction.data then return end
      local card = Fk:cloneCard(self.interaction.data)
      card:addSubcards(cards)
      card.skillName = fangxu.name
      return card
    end,
    before_use = function(self, player, use)
       local room=player.room
       local choices={"fangxu_damage","fangxu_draw"}
       local ret=room:askToChoice(player, {
        skill_name = fangxu.name,
        choices=choices,
        prompt = "#history__fangxu-choice",
       })
       use.extra_data = use.extra_data or {}
       use.extra_data.fangxu_result = ret
    end,
    after_use = function(self, player, use)
        local room=player.room
        if use.extra_data and  use.extra_data.fangxu_result then
            local ret = use.extra_data.fangxu_result
            if ret == "fangxu_damage" then
               if use.damageDealt then
                for to, dam in pairs(use.damageDealt) do 
                    local card = room:askToChooseCards(player, {
                        target = to,
                        flag = "he",
                        min = 0,
                        max = 2,
                        skill_name = fangxu.name,
                      })
                      room:throwCard(card, fangxu.name, to, player)
                end
               end
            elseif ret == "fangxu_draw" then
                if not use.damageDealt then
                    local current =room.current
                    if current then
                        current:drawCards(2, fangxu.name)
                    end
                end
            end
        end
    end,
    enabled_at_play = function(self, player)
        return player:usedSkillTimes(fangxu.name, Player.HistoryRound)<2 and
          #player:getViewAsCardNames(fangxu.name, {"slash", "jink"}) > 0
      end,
    enabled_at_response = function(self, player, response)
      return false
    end,
  })




fangxu:addEffect(fk.AfterCardsMove, {
    refresh_events = { fk.AfterCardsMove},
    can_refresh = function(self, event, target, player, data)
    if player:hasSkill(fangxu.name) then
        for _, move in ipairs(data) do
          return move.to == player and move.toArea == Player.Hand
        end
      end
    end,
    on_refresh = function(self, event, target, player, data)
      local room = player.room
      for _, move in ipairs(data) do
        for _, info in ipairs(move.moveInfo) do
          local card = Fk:getCardById(info.cardId)
          room:setCardMark(card, "@@history__fangxu_mark-turn", 1)
        end
      end
    end,
  })


return fangxu